<?php
/*******************************************************************************
mobileReservesDisplayer.class.php
Display code for reserve pages on mobile devices.


Created by Karl Doerr, NCSU Libraries, NC State University (libraries.opensource@ncsu.edu).

This file is part of NCSU's distribution of ReservesDirect. This version has not been downloaded from Emory University
or the original developers of ReservesDirect. Neither Emory University nor the original developers of ReservesDirect have authorized
or otherwise endorsed or approved this distribution of the software.

Copyright (c) 2004-2006 Emory University, Atlanta, Georgia.

Licensed under the NCSU ReservesDirect License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License.You may obtain a copy of the full License at
 http://www.lib.ncsu.edu/it/opensource/

ReservesDirect is distributed in the hope that it will be useful,
but is distributed "AS IS" and WITHOUT ANY WARRANTY, without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE, and without any warranty as to non-infringement of any third
party's rights.See the License for the specific language governingpermissions and limitations under the License.

The original version of ReservesDirect is located at:
http://www.reservesdirect.org/

This version of ReservesDirect, distributed by NCSU, is located at:
http://code.google.com/p/reservesdirect-ncsu/

*******************************************************************************/
require_once("secure/common.inc.php");
require_once('secure/displayers/noteDisplayer.class.php');
require_once('secure/classes/tree.class.php');
require_once('secure/classes/reserves.class.php');

class mobileReservesDisplayer extends noteDisplayer {
	
	public function displayReserves($cmd, &$ci, &$tree_walker, $reserve_count, &$hidden_reserves=null, $preview_only=false) {
  		global $u;
        
		if(!($ci->course instanceof course)) {
			$ci->getPrimaryCourse();
		}
		
		//if previewing, temporarily give the current user a role of student
		//Note: this process is reversed at the end of this method.
        
        // announce rss feed to capable browsers
        echo "<link rel=\"alternate\" title=\"{$ci->course->department->name} {$ci->course->courseNo} {$ci->term} {$ci->year}\" href=\"rss.php?ci={$ci->courseInstanceID}\" type=\"application/rss+xml\"/>\n";
?>

				
		
		<form method="post" name="editReserves" action="index.php">
		
			<input type="hidden" name="cmd" value="<?=$cmd?>" />
			<input type="hidden" name="ci" value="<?=$ci->getCourseInstanceID()?>" />
			<input type="hidden" name="hideSelected" value="" />
			<input type="hidden" name="showAll" value="" />

		<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" style="clear: both;">
			<tr>
				<td colspan="2">
					<ul class="reserveList"> 
			
<?php
		//begin displaying individual reserves
		//loop
		$prev_depth = 0;
		foreach($tree_walker as $leaf) {
			//close list tags if backing out of a sublist
			if($prev_depth > $tree_walker->getDepth()) {
				echo str_repeat('</ul><ul class="reserveList">', ($prev_depth-$tree_walker->getDepth()));
			}
			
		
			$reserve = new reserve($leaf->getID());	//init a reserve object

			//is this item hidden?
			$reserve->hidden = in_array($leaf->getID(), $hidden_reserves) ?	true : false;

			//display the info
			echo ($leaf->hasChildren()) ? '</li></ul><ul class="reserveList">' : '';
			echo '<li>';
			self::displayReserveRowView($reserve, '');
			
			//start sublist or close list-item?
			//echo ($leaf->hasChildren()) ? '</li></ul><ul>' : '</li>';
			echo '</li>';
			
			$prev_depth = $tree_walker->getDepth();
		}
		echo str_repeat('</ul></li>', ($prev_depth));	//close all lists
?>

					</ul>
				</td>
			</tr>
		<!--  	
			<tr valign="middle">
				<td class="headingCell1" align="center" colspan="2">
					&nbsp;
				</td>
			</tr>
		-->
		</table>
		</form>
		
		<p />
		<div style="margin-left:5%; margin-right:5%; text-align:right;"><strong><?=$exit_class_link?></strong></div>
<?php

		//if previewing, return user to original state
		if($preview_only) {
			$u = $curr_user;
		}
	}
	
	public function displayReserveRowView(&$reserve, $block_style='') {
		if(!($reserve->item instanceof reserveItem)) {
			$reserve->getItem();	//pull in item info
		}
		
		$checkbox_onchange = '';
		if($reserve->item->isHeading()) {	//style as heading
			$block_style ='class="headingCell2"';
			$checkbox_onchange = 'onchange="javascript:alert(\'Checking/Unchecking this box will hide/unhide everything in this folder\')"';
		}
		
		$checkbox_checked = ((isset($reserve->hidden) && $reserve->hidden) || (isset($reserve->selected) && $reserve->selected)) ? 'checked="checked"' : '';
?>
	
	<div <?=$block_style?>>

<?php	if($reserve->hidden): ?>
		<div class="hiddenItem">
<?php	endif; ?>
			
			
		
			<?php self::displayReserveInfo($reserve, 'class="metaBlock-wide"'); ?>

<?php	if($reserve->hidden): ?>
		</div>
<?php	endif; ?>

		<!-- hack to clear floats -->
		<div style="clear:both;"></div>
		<!-- end hack -->
	</div>
<?php
	}
	
	public function displayReserveInfo(&$reserve, $meta_style='') {
		global $u, $g_mReservesViewer;

		if(!($reserve->item instanceof reserveItem)) {
			$reserve->getItem();	//pull in item info
		}
		
		$notes = $reserve->getNotes(true);	//get notes
		
		if($reserve->item->isPhysicalItem()){
			$viewReserveURL = "class='arrow' href='". $g_mReservesViewer . $reserve->item->getLocalControlKey() ."'";
		}
		elseif($reserve->item->isVideoItem()){
			$viewReserveURL = "";
		}
		else{
			$viewReserveURL = "class='arrow' href='reservesViewer.php?reserve=" . $reserve->getReserveID() ."'";
		}
		if(!$reserve->item->isHeading()){
?> 
		<a <?=$viewReserveURL?>" target="_blank" class="itemTitle">
<?php
		}
		
		#=== heading ===#
		
		//only show basic info about a heading
		if($reserve->item->isHeading()):
?>
		
		<div class="headingText" style="border:0px solid red;">
<?php
			echo $reserve->item->getTitle();
			//show notes
			noteDisplayer::displayNotes($notes);
?>
		</div>
<?php
			return;	//nothing else to show, so just return
		endif;
		
		#=== reserve ===#
		
		//for non-headings show all the meta info
		$title = $reserve->item->getTitle();
		$author = $reserve->item->getAuthor();
		$url = $reserve->item->getURL();
		$performer = $reserve->item->getPerformer();
		$volTitle = $reserve->item->getVolumeTitle();
		$volEdition = $reserve->item->getVolumeEdition();
		$pagesTimes = $reserve->item->getPagesTimes();
		$source = $reserve->item->getSource();
		$itemIcon = $reserve->item->getItemIcon();
		
		
		//for physical items, pull in some other info
		if($reserve->item->isPhysicalItem()) {
			$reserve->item->getPhysicalCopy();	//get physical copy info
			$callNumber = $reserve->item->physicalCopy->getCallNumber();
			//get home library/reserve desk
			$lib = new library($reserve->item->getHomeLibraryID());
			$reserveDesk = $lib->getReserveDesk();
			
			
		}
?>
		<div class="iconBlock">
			<img src="<?=$itemIcon?>" alt="icon" border="0">&nbsp;
		</div>

		<div <?=$meta_style?>>
		
<?php	if($reserve->item->isPhysicalItem()): ?>	
		
			<span class="itemTitleNoLink"><?=$title?></span>
			<br />
			<span class="itemAuthor"><?=$author?></span>
			<br />
			<span class="itemMeta"><?=$callNumber?></span>
			<br />
			<span class="itemMetaPre">On Reserve at:</span><span class="itemMeta"><?=$reserveDesk?></span>
			
<?php	else: ?>
			<?php if ($reserve->getStatus() == 'ACTIVE' || $reserve->getStatus() == 'INACTIVE'): ?>
				 <span class="title"><?=$title?></span>
			<?php	else: ?>
				<span class="itemTitleNoLink"><?=$title?></span>
			<?php	endif; ?>
			<br />
			<span class="itemAuthor"><?=$author?></span>
			
<?php	endif; ?>


<?php	if($performer): ?>

       		<br />
       		<span class="itemMetaPre">Performed by: </span><span class="itemMeta"><?=$performer?></span>
       		
<?php	endif; ?>
<?php	if($volTitle): ?>

       		<br />
       		<span class="itemMetaPre">From: </span><span class="itemMeta"><?=$volTitle?></span>
       		
<?php	endif; ?>
<?php	if($volEdition): ?>

       		<br />
       		<span class="itemMetaPre">Volume/Edition: </span><span class="itemMeta"><?=$volEdition?></span>
       		
<?php	endif; ?>
<?php	if($pagesTimes): ?>

       		<br />
       		<span class="itemMetaPre">Pages/Time: </span><span class="itemMeta"><?=$pagesTimes?></span>
       		
<?php	endif; ?>
<?php	if($source): ?>

       		<br />
       		<span class="itemMetaPre">Source/Year: </span><span class="itemMeta"><?=$source?></span>
       		
<?php	endif; ?>

<?php
		//show notes
		noteDisplayer::displayNotes($notes);
			
		//show additional info
		if(!empty($reserve->additional_info)) {
			echo $reserve->additional_info;
		}
?>
		</div>
<?php 
if(!$reserve->item->isHeading()){
?>		
		</a>
<?php }?>
<?php
	}
  
}